// Handle modal
App.directive('edModal', [function () {

    return {

        restrict: 'A',

        replace: true,

        transclude: true,

        templateUrl: 'views/directive/modal.html',

        link: function (scope, elem, attrs, controller, transcludeFn) {
    		var option = AppUtil.jsonfy(attrs.options);
    		if(!scope[attrs.edModal]){
    			scope[attrs.edModal] = {};
    		}
    		angular.extend(scope[attrs.edModal], option);
    		var modal = scope[attrs.edModal];
    		
            elem.find('.modal-title:eq(0)').html(modal.title);
            elem.find('.fa:eq(0)').addClass(modal.icon || '');

            // 宽度
            if (modal.width) {
                elem.attr('data-width', modal.width);
            }
            // 宽度
            if (modal.static === true) {
                elem.attr('data-backdrop', 'static');
            }

            // 是否显示默认按钮
            var footer = elem.children('.modal-footer:eq(0)');
            if (modal.defaultButton === false) {
                footer.hide();
                // 隐藏事件
                $(elem).on('hidden', function () {// 取消表单校验状态
                    //elem.draggable('destroy');// 取消拖动
                    var form = elem.find('form[w5c-form-validate]');
                    if (form.length > 0 && scope[form.attr('name')]) {
                        scope[form.attr('name')].reset();
                    }
                });
            }

            transcludeFn(scope, function(cElement) {
                elem.find('.modal-body:eq(0)').append(cElement);
            });

            // 设置title
            modal.setTitle = function(title) {
                elem.find('.modal-title:eq(0)').html(title);
                return modal;
            }

            // 显示
            modal.show = function() {
                // 允许拖动
                elem.draggable({
                    handle: ".modal-header",
                    containment: 'parent'
                });
                $(elem).modal('show');
            }
            // 显示事件
            if (!angular.isUndefined(modal.onShow)) {// 定义了显示事件
                $(elem).on('show', function () {
                    if (angular.isFunction(modal.onShow)) {// 指定方法
                        modal.onShow(elem);
                    } else if (angular.isFunction(modal[modal.onShow])) {// 指定方法名
                        modal[modal.onShow](elem);
                    } else if (angular.isFunction(scope[modal.onShow])) {// 指定方法名
                        scope[modal.onShow](elem);
                    }
                });
            }

            // 隐藏
            modal.hide = function() {
                $(elem).modal('hide');
            }
            // 隐藏事件
            if (!angular.isUndefined(modal.onHidden)) {// 定义了隐藏事件
                $(elem).on('hidden', function () {
                    //elem.draggable('destroy');// 取消拖动
                    if (angular.isFunction(modal.onHidden)) {// 指定方法
                        modal.onHidden(elem);
                    } else if (angular.isFunction(modal[modal.onHidden])) {// 指定方法名
                        modal[modal.onHidden](elem);
                    } else if (angular.isFunction(scope[modal.onHidden])) {// 指定方法名
                        scope[modal.onHidden](elem);
                    }
                });
            }

            // ok
            footer.find('.btn-confirm').click(function() {
                scope.$apply(function() {
                    var ret;
                    if (!angular.isUndefined(modal.onOkClick)) {// 定义了ok事件
                        if (angular.isFunction(modal.onOkClick)) {// 指定方法
                            ret = modal.onOkClick(elem);
                        } else if (angular.isFunction(modal[modal.onOkClick])) {// 指定方法名
                            ret = modal[modal.onOkClick](elem);
                        } else if (angular.isFunction(scope[modal.onOkClick])) {// 指定方法名
                            ret = scope[modal.onOkClick](elem);
                        }
                    }
                    //if (ret !== false) {
                    //    modal.hide();
                    //}
                });
            });
        }

    };
}]);